package com.education.wisdom.service.impl;


import com.education.wisdom.mapper.CatalogMapper;
import com.education.wisdom.pojo.Catalog;
import com.education.wisdom.service.ICatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 目录体系 服务层实现
 *
 * @author ruoyi
 * @date 2018-11-15
 */
@Service
public class CatalogServiceImpl implements ICatalogService {
    @Autowired
    private CatalogMapper catalogMapper;

    @Override
    public Catalog selectByCatalogId2(Integer catalogId) {
        return catalogMapper.selectByCatalogId2(catalogId);
    }

    @Override
    public List<Catalog> isBuy(Integer userId, List<Integer> catalogId, String name, Integer schoolId) {
        return catalogMapper.isBuy(userId, catalogId, name, schoolId);
    }

    @Override
    public List<Catalog> selectCatalogBySchoolId(Integer schoolId) {
        return catalogMapper.selectCatalogBySchoolId(schoolId);
    }

    @Override
    public List<Catalog> isNotBuy(Integer userId, List<Integer> catalogId, String name, Integer schoolId) {
        return catalogMapper.isNotBuy(userId, catalogId, name, schoolId);
    }

    @Override
    public Catalog selectCatalogInfoById(Integer parentId) {
        return catalogMapper.selectCatalogInfoById(parentId);
    }

    @Override
    public List<Catalog> selectByCatalogId(Integer catalogId) {
        return catalogMapper.selectByCatalogId(catalogId);
    }

    @Override
    public List<Catalog> selectCatalogList2(Catalog catalog) {
        return catalogMapper.selectCatalogList2(catalog);
    }

    /**
     * 试卷展示页面左侧课程展示
     *
     * @param map
     * @return
     */
    @Override
    public Catalog leftClassInfo(Map<String, Object> map) {
        return catalogMapper.leftClassInfo(map);
    }

    @Override
    public List<Catalog> selectCatalogById(Map<String, Object> map) {
        return catalogMapper.selectCatalogById(map);
    }

    /**
     * 精品课程展示
     *
     * @return
     */
    @Override
    public List<Catalog> bestClass(Map<String, Object> map) {
        return catalogMapper.bestClass(map);
    }

    @Override
    public List<Catalog> selectMyCollection(String userId) {
        return catalogMapper.selectMyCollection(userId);
    }

    /**
     * 热门课程展示
     *
     * @return
     */
    @Override
    public List<Catalog> hotClass(Map<String, Object> map) {
        return catalogMapper.hotClass(map);
    }


    /**
     * 右侧热门课程展示
     *
     * @return
     */
    @Override
    public List<Catalog> rightHot() {
        return catalogMapper.rightHot();
    }

    @Override
    public List<Catalog> selectCatalogList(Catalog catalog) {
        return catalogMapper.selectCatalogList(catalog);
    }

    /**
     * 根据课程名称进行查询
     *
     * @param catalog
     * @return
     */
    @Override
    public List<Catalog> selectCatalogListByName(Catalog catalog) {
        return catalogMapper.selectCatalogListByName(catalog);
    }

    /**
     * 右侧免费好课展示
     *
     * @return
     */
    @Override
    public List<Catalog> rightFree() {
        return catalogMapper.rightFree();
    }

    /**
     * 免费好课展示
     *
     * @return
     */
    @Override
    public List<Catalog> freeClass() {
        return catalogMapper.freeClass();
    }


    /**
     * 最新课程展示
     *
     * @return
     */
    @Override
    public List<Catalog> newClass(Map<String, Object> map) {
        return catalogMapper.newClass(map);
    }

    @Override
    public List<Catalog> CurriculumLists() {
        return catalogMapper.CurriculumLists();
    }

    /**
     * 查询课程详情
     *
     * @param catalogId
     * @return
     */
    @Override
    public Catalog classInfo(Integer catalogId) {
        return catalogMapper.classInfo(catalogId);
    }

    @Override
    public List<Catalog> more(Map<String, Object> map) {
        return catalogMapper.more(map);
    }

    @Override
    public List<Catalog> hot(Map<String, Object> map) {
        return catalogMapper.hot(map);
    }

    @Override
    public List<Catalog> allList(Map<String, Object> map) {
        return catalogMapper.allList(map);
    }

    @Override
    public List<Catalog> newLists(Map<String, Object> map) {
        return catalogMapper.newLists(map);
    }

    @Override
    public List<Catalog> selectBuyListByUserId(String userId) {
        return catalogMapper.selectBuyListByUserId(userId);
    }

    @Override
    public List<Catalog> selectListByUserId(String userId) {
        return catalogMapper.selectListByUserId(userId);
    }

    @Override
    public List<Catalog> hotLists(Map<String, Object> map) {
        return catalogMapper.hotLists(map);
    }

    @Override
    public List<Catalog> selectByUser(Map<String, Object> map) {
        return catalogMapper.selectByUser(map);
    }

    @Override
    public int delectCataLogById(String catalogId) {
        return catalogMapper.deleteByCatalogId(catalogId);
    }

    @Override
    @Transactional
    public int updateCataLog(Catalog catalog) {
        return catalogMapper.updateCataLog(catalog);
    }

    @Override
    public List<Catalog> classLista(Map<String, Object> map) {
        return catalogMapper.classLista(map);
    }

    @Override
    public List<Catalog> classListb(Map<String, Object> map) {
        return catalogMapper.classListb(map);
    }

    @Override
    public List<Catalog> selectCatalogLists(String uId) {
        return catalogMapper.selectCatalogLists(uId);
    }

    @Override
    public List<Catalog> selectBig() {
        return catalogMapper.selectBig();
    }

    @Override
    public List<Catalog> selectBig2() {
        return catalogMapper.selectBig2();
    }

    @Override
    public List<Catalog> selectBySmall(Integer catalogId) {
        return catalogMapper.selectBySmall(catalogId);
    }

    @Override
    public List<Catalog> selectBySmall2(Integer catalogId) {
        return catalogMapper.selectBySmall2(catalogId);
    }

    @Override
    public List<Catalog> selectCatalogByLeft(Map<String, Object> map) {
        return catalogMapper.selectCatalogByLeft(map);
    }

    @Override
    public List<Catalog> selectCategoriesList() {
        return catalogMapper.selectCategoriesList();
    }

    /**
     * 通过ParentId 和 schoolId 查询数据
     *
     * @param parentId
     * @return
     */
    @Override
    public List<Catalog> selectByParentIdThree(Integer parentId, Integer schoolId) {
        return catalogMapper.selectByParentIdThree(parentId, schoolId);
    }

    /**
     * 通过ParentId查询数据
     *
     * @param parentId
     * @return
     */
    @Override
    public List<Catalog> selectByParentId2(Integer parentId) {
        return catalogMapper.selectByParentId2(parentId);
    }

    /**
     * 通过ParentId查询数据
     *
     * @param ParentId
     * @return
     */
    @Override
    public List<Catalog> selectByParentId(Integer ParentId) {
        return catalogMapper.selectByParentId(ParentId);
    }

    @Override
    public Integer[] selectByCatalogIdParentId(Integer ParentId) {
        return catalogMapper.selectByCatalogIdParentId(ParentId);
    }

    @Override
    public List<Catalog> selectByParentIdsAndSort(List<Integer> parentId, Integer sort) {
        return catalogMapper.selectByParentIdsAndSort(parentId, sort);
    }

    @Override
    public List<Catalog> selectByParentIdAndSort(Integer ParentId, Integer sort) {
        return catalogMapper.selectByParentIdAndSort(ParentId, sort);
    }

    @Override
    public List<Catalog> selectByCatalogIdAndSort(Integer catalog, Integer sort) {
        return catalogMapper.selectByCatalogIdAndSort(catalog, sort);
    }

    /**
     * 根据目录等级查询列表
     *
     * @param i 等级
     * @return
     */
    @Override
    public List<Catalog> selectcatalogBySort(Integer i) {
        return catalogMapper.selectcatalogBySort(i);
    }

    @Override
    public Catalog selectCatalogByCatalogId(Integer catalogId) {
        return catalogMapper.selectCatalogByCatalogId(catalogId);
    }

    /**
     * 根据课程id 查找所需字段
     *
     * @param catalogId
     * @return
     */
    @Override
    public Catalog selectCatalogByCatalogIdToBuy(Integer catalogId) {
        return catalogMapper.selectCatalogByCatalogIdToBuy(catalogId);
    }

    @Override
    public void insertCatalog(Catalog catalog) {
        catalogMapper.insertCatalog(catalog);
    }

    @Override
    public List<Catalog> selectByUserIdAndCatalogId(Map<String, Object> map) {
        return catalogMapper.selectByUserIdAndCatalogId(map);
    }

    @Override
    public List<Catalog> selectList(Map<String, Object> map) {
        return catalogMapper.selectList(map);
    }

    @Override
    public List<Catalog> selectByLikeName(String name) {
        return catalogMapper.selectByLikeName(name);
    }

    @Override
    public List<Catalog> selectByThreeCatalogId() {
        return catalogMapper.selectByThreeCatalogId();
    }

    @Override
    public List<Catalog> selectTwoCatalogList() {
        return catalogMapper.selectTwoCatalogList();
    }

    @Override
    public List<Catalog> selectBySort(Integer sort) {
        return catalogMapper.selectBySort(sort);
    }

    @Override
    public List<Map<String, Object>> selectCatalogTree() {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        List<Catalog> catalogList = selectCatalogList2(new Catalog());
        trees = getTrees(catalogList, false, null);
        return trees;
    }

    @Override
    public int deleteByCatalogId2(Integer id) {
        return catalogMapper.deleteByCatalogId2(id);
    }

    @Override
    public List<Catalog> selectCatalogByOne(Integer catalogId, String name) {
        return catalogMapper.selectCatalogByOne(catalogId, name);
    }

    /**
     * 查询学校线下购买课程信息
     */
    @Override
    public List<String> isBuyOffline(List<Integer> list) {
        return catalogMapper.isBuyOffline(list);
    }

    @Override
    public List<Catalog> selectLikeByUserId(Integer userId) {
        return catalogMapper.selectLikeByUserId(userId);
    }

    @Override
    public Integer[] selectCatalogIds() {
        return catalogMapper.selectCatalogIds();
    }

    /**
     * 查询全部二级目录Integer List
     *
     * @return
     */
    @Override
    public List<Integer> selectALLTwoLevelMenus() {
        return catalogMapper.selectALLTwoLevelMenus();
    }

    @Override
    public List<Catalog> buyCatalog(Integer userId) {
        return catalogMapper.buyCatalog(userId);
    }

    /**
     * 对象转目录树
     *
     * @param catalogList     目录列表
     * @param isCheck         是否需要选中
     * @param roleCatalogList 角色已存在菜单列表
     * @return
     */
    public List<Map<String, Object>> getTrees(List<Catalog> catalogList, boolean isCheck, List<String> roleCatalogList) {

        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (Catalog catalog : catalogList) {
			/*if (UserConstants.DEPT_NORMAL.equals(catalog.getStatus()))
			{*/
            Map<String, Object> deptMap = new HashMap<String, Object>();
            deptMap.put("id", catalog.getCatalogId());
            deptMap.put("pId", catalog.getParentId());
            deptMap.put("name", catalog.getName());
            deptMap.put("title", catalog.getName());
            if (isCheck) {
                deptMap.put("checked", roleCatalogList.contains(catalog.getCatalogId() + catalog.getName()));
            } else {
                deptMap.put("checked", false);
            }
            trees.add(deptMap);
            //}
        }
        return trees;
    }
}
